package com.magneticattraction.testnfc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.magneticattraction.service.OpenOrCloseAidlInterface;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private static final String TAG = "MainActivity-------";

    private OpenOrCloseAidlInterface openOrCloseAidlInterface;

    private Switch aSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //switch 点击事件
        aSwitch.setOnCheckedChangeListener(this);


        //AIDL

        Intent serviceIntent = new Intent("android.intent.action.ReceiveAndSendService");
        serviceIntent.setPackage("com.magneticattraction.service");

        bindService(serviceIntent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                System.out.println("service正常绑定");
                openOrCloseAidlInterface = OpenOrCloseAidlInterface.Stub.asInterface(service);
                System.out.println(openOrCloseAidlInterface);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                System.out.println("service断开连接");
                openOrCloseAidlInterface = null;
            }
        },BIND_AUTO_CREATE);

        Button btn_aidl = (Button)findViewById(R.id.btn_aidl);
        btn_aidl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Boolean if_open = openOrCloseAidlInterface.openOrClose(1);
                    System.out.println("AIDL传的值："+if_open);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });



    }

    //初始化控件
    private void initView() {
        aSwitch = findViewById(R.id.switchBar);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //通过判断switch的状态，去调用AIDL接口实现开关控制
        if (isChecked) {


            //测试接收广播
            SensorReceiver sensorReceiver = new SensorReceiver();
            //Toast.makeText(this, "receiver"+sensorReceiver.getSensor(), Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onCreate: ++++SensorReceiver"+sensorReceiver.getSensor());
            System.out.println("onCreate: ++++SensorReceiver"+sensorReceiver.getSensor());



            //打开
            //Toast.makeText(this, "open is:"+isChecked, Toast.LENGTH_SHORT).show();
            //Log.e(TAG,"onCheckedChanged() status is:"+isChecked);
        }else {
            //关闭
            //Toast.makeText(this, "close is:"+isChecked, Toast.LENGTH_SHORT).show();
            //Log.e(TAG,"onCheckedChanged() status is:"+isChecked);
        }
    }
}